{extend name="app/shop/view/base.html"/}
{block name="resources"}
<style>
    .layui-form-selected dl{z-index: 1000;}
    .multiple-uploading{display: flex;flex-wrap: wrap;}
    .multiple-uploading .multiple-item{position: relative;display: flex;justify-content: center;align-items: center;flex-direction: column;margin-bottom: 10px;padding: 10px;width: 100px;height: 100px;border: 1px dashed #ddd;text-align: center;box-sizing: border-box;line-height: 1;color: #5a5a5a;}
    .multiple-uploading .multiple-item p{line-height: 20px;}
    .multiple-uploading .multiple-item span{display: none;position: absolute;top: -10px;right: -7px;width: 20px;height: 20px;border: 1px solid #999;color: #999;background-color: #fff;border-radius: 50%;line-height: 1;font-size: 18px;cursor: pointer;}
    .multiple-uploading .multiple-item:hover .icon{display: block;}
    .multiple-uploading .multiple-item ~ .multiple-item{margin-left: 10px;}
    #multiple_uploading{cursor: pointer;}
    .multiple-item img{max-width: 100%;max-height: 100%;}
    .shopping-lightspot{width: 290px;}
    .shopping-lightspot .add-lightspot{cursor: pointer;}
    .shopping-lightspot .add-lightspot, .shopping-lightspot .lightspot-item{padding: 15px;border: 1px dashed #e5e5e5;line-height: 1;}
    .shopping-lightspot .lightspot-item{position: relative;display: flex;align-items: center;margin-bottom: 10px;white-space: nowrap;}
    .shopping-lightspot .lightspot-item span{position: absolute;top: -10px;right: -9px;display: none;text-align: center;width: 20px;height: 20px;border: 1px solid #999;border-radius: 50%;font-size: 18px;color: #999;background: #fff;}
    .shopping-lightspot .lightspot-item:hover span{display: block;cursor: pointer;}
    .shopping-lightspot .lightspot-item input{margin-left: 5px;}
</style>
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
    <div class="layui-colla-item">
        <h2 class="layui-colla-title">操作提示</h2>
        <ul class="layui-colla-content layui-show">
            <li>管理员可以在此页编辑店铺笔记</li>
        </ul>
    </div>
</div>

<div class="layui-form ns-form">

    <div class="layui-form-item">
        <label class="layui-form-label img-upload-lable ns-short-label"><span class="required">*</span>笔记标题：</label>
        <div class="layui-input-inline">
            <input type="text" name="note_title" value="{$info.note_title}" lay-verify="required" maxlength="40" autocomplete="off" placeholder="笔记标题最多40个字" class="layui-input ns-len-long">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label img-upload-lable ns-short-label">摘要：</label>
        <div class="layui-input-block">
            <textarea name="note_abstract" class="layui-textarea ns-len-long" maxlength="100">{$info.note_abstract}</textarea>
        </div>
        <div class="ns-word-aux">笔记摘要最多可输入100个字</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>封面：</label>
        <div class="layui-input-inline">
            <input type="radio" name="cover_type" value="0" title="单图" lay-filter="cover_type" {if $info.cover_type == 0} checked {/if}>
            <input type="radio" name="cover_type" value="1" title="多图" lay-filter="cover_type" {if $info.cover_type == 1} checked {/if}>
        </div>
    </div>

    <!-- 图片上传 -->
    <div class="layui-form-item">
        <label class="layui-form-label img-upload-lable"></label>
        <div class="layui-input-block img-upload">
            <div class="upload-img-block square simple-uploading {if $info.cover_type != 0}layui-hide{/if}">
                <div class="upload-img-box" id="img">
                    <div class="ns-upload-default">
                        <img src="SHOP_IMG/upload_img.png" />
                        <p>点击上传132</p>
                    </div>
                </div>
            </div>

            <div class="multiple-uploading {if $info.cover_type != 1}layui-hide{/if}">
                <div class="multiple-item" id="multiple_uploading">
                    <img src="SHOP_IMG/upload_img.png" />
                    <p>点击上传</p>
                </div>
            </div>
        </div>
        <div class="ns-word-aux">推荐使用 750x420 像素的图片 最多上传1张</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>笔记分组：</label>
        <div class="layui-input-inline">
            <select name="group_id" lay-verify="required">
                <option value="">请选择</option>
                {foreach $group_list as $v}
                <option value="{$v['group_id']}" {if $info.group_id == $v.group_id} selected {/if}>{$v['group_name']}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label img-upload-lable ns-short-label"><span class="required">*</span>选择商品：</label>
        <div class="layui-input-inline">
            <div class="upload-img-block square">
                <div class="upload-img-box" id="goodImg" lay-verify="select" onclick="addGoods()">
                    <img src="{if !empty($info.goods_list)}{:img($info.goods_list[0]['goods_image'])}{/if}" alt="">
                </div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>商品亮点：</label>
        <div class="layui-input-block shopping-lightspot">
            <div class="add-lightspot">+ 添加亮点</div>
        </div>
        <div class="ns-word-aux">商品亮点最多可添加3个亮点</div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>笔记内容：</label>
        <div class="layui-input-inline">
            <script id="editor" type="text/plain" class="ns-special-length" style="height:500px;"></script>
            <input type="hidden" name="note_content" id="note_content" value="{$info.note_content}"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">发布时间：</label>
        <div class="layui-input-inline">
            <input type="radio" name="is_show_release_time" value="1" title="显示" {if $info.is_show_release_time == 1} checked {/if}>
            <input type="radio" name="is_show_release_time" value="0" title="不显示" {if $info.is_show_release_time == 0} checked {/if}>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">阅读次数：</label>
        <div class="layui-input-inline">
            <input type="radio" name="is_show_read_num" value="1" title="显示" {if $info.is_show_read_num == 1} checked {/if}>
            <input type="radio" name="is_show_read_num" value="0" title="不显示" {if $info.is_show_read_num == 0} checked {/if}>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">点赞次数：</label>
        <div class="layui-input-inline">
            <input type="radio" name="is_show_dianzan_num" value="1" title="显示" {if $info.is_show_dianzan_num == 1} checked {/if}>
            <input type="radio" name="is_show_dianzan_num" value="0" title="不显示" {if $info.is_show_dianzan_num == 0} checked {/if}>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">虚拟阅读数：</label>
        <div class="layui-input-inline">
            <input type="number" min="0" name="initial_read_num" value="{$info.initial_read_num}"  class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">虚拟点赞数：</label>
        <div class="layui-input-inline">
            <input type="number" min="0" name="initial_dianzan_num" value="{$info.initial_dianzan_num}" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">排序：</label>
        <div class="layui-input-inline">
            <input type="number" min="0" name="sort" value="{$info.sort}" class="layui-input">
        </div>
    </div>

    <input type="hidden" name="note_id" value="{$info.note_id}" />
    <input type="hidden" name="goods_ids" value="{$info.goods_ids}" />
    <input type="hidden" name="note_type" value="{$info.note_type}" />

    <div class="ns-form-row">
        {if $info.status == 1}
        <button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
        {else /}
        <button class="layui-btn ns-bg-color" lay-submit lay-filter="saveDrafts">保存</button>
        <button class="layui-btn ns-bg-color" lay-submit lay-filter="save">立即发布</button>
        <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
        {/if}
    </div>

</div>

<!-- 操作 -->
<script type="text/html" id="operation">
    <div class="ns-table-btn">
        <a class="layui-btn" onclick="delGoods({{d.goods_id}})">删除</a>
    </div>
</script>

{/block}
{block name="script"}
<script type="text/javascript" charset="utf-8" src="STATIC_EXT/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="STATIC_EXT/ueditor/ueditor.all.js"></script>
<script type="text/javascript" charset="utf-8" src="STATIC_EXT/ueditor/lang/zh-cn/zh-cn.js"></script>
<script>
    var form,
            IMAGE_MAX = 9, //最多可以上传多少张图片
            imageCollection = [], //图片集合
            repeat_flag = false,
            coverType = '{$info.cover_type}',
            coverImg = '{$info.cover_img}',
            goods_ids = '{$info.goods_ids}';

    initImg();//初始化图片
    function initImg(){
        if (coverType == 0){
            imageCollection.push(coverImg);
            //单图
            $("#img").html("<img src=" + ns.img(coverImg) + " >");
        } else if (coverType == 1) {
            //多图
            var coverImgArr = coverImg.split(','),
                    html = '';

            imageCollection = coverImgArr;
            for (var i = 0; i < coverImgArr.length; i++) {
                html += `<div class="multiple-item">`;
                html += `<img src="${ns.img(coverImgArr[i])}" alt="">`;
                html += `<span class="icon">x</span>`;
                html += `</div>`;
            }

            $(".multiple-uploading").append(html);
        }
    }
    //初始化商品亮点
    var spotGoods = "{$info.goods_highlights}";
    initShoppingLightspot();
    function initShoppingLightspot() {
        if (!spotGoods) return false;

        spotGoods = spotGoods.split(',');

        var html = '';

        for(var i = 0; i < spotGoods.length; i++){
            html += `<div class="lightspot-item">`;
            html += `亮点名称：<input type="text" class="layui-input" maxlength="10" placeholder="最多10个字" value="${spotGoods[i]}">`;
            html += `<span>x</span>`;
            html += `</div>`;
        }
        if (spotGoods.length == 3) $(".shopping-lightspot").html(html);
        else $(".shopping-lightspot .add-lightspot").before(html)
    }

    //实例化富文本
    var ue = UE.getEditor('editor');
    if($("#note_content").val()){
        ue.ready(function() {
            ue.setContent($("#note_content").val());
        });
    }

    layui.use(['form'], function() {
        form = layui.form;
        form.render();

        /**
         * 图片切换
         */
        form.on('radio(cover_type)', function(data){
            //0单图  1多图
            if (data.value == 0){

                if(imageCollection.length) {
                    imageCollection.splice(1, imageCollection.length);
                    var val = '<img src="' + ns.img(imageCollection[0]) + '" alt="">';
                    $("#img").html(val);
                }

                $(".simple-uploading").removeClass("layui-hide");
                $(".multiple-uploading").addClass("layui-hide");
                $(".simple-uploading").parents(".layui-form-item").find('.ns-word-aux').text('推荐使用 750x420 像素的图片 最多上传1张');
            } else{
                if(imageCollection.length) {
                    var html = '';
                    html += `<div class="multiple-item" id="multiple_uploading">`;
                    html += `<img src="SHOP_IMG/upload_img.png" />`;
                    html += `<p>点击上传</p>`;
                    html += `</div>`;

                    for (var i = 0; i < imageCollection.length; i++) {
                        html += `<div class="multiple-item">`;
                        html += `<img src="${ns.img(imageCollection[i])}" alt="">`;
                        html += `<span class="icon">x</span>`;
                        html += `</div>`;
                    }


                    $(".multiple-uploading").html(html);
                }

                $(".simple-uploading").addClass("layui-hide");
                $(".multiple-uploading").removeClass("layui-hide");

                $(".simple-uploading").parents(".layui-form-item").find('.ns-word-aux').text('推荐使用 350x350 像素的图片，最多上传9张');
            }
        });

        // 单图上传
        $("body").on("click", "#img", function () {
            var html = '';
            openAlbum(function (data) {
                imageCollection = [];
                imageCollection.push(data[0].pic_path);
                imageCollection.splice(1, imageCollection.length);
                var val = '<img src="' + ns.img(imageCollection[0]) + '" alt="">';
                $("#img").html(val);
            }, 1);
        });

        //多图上传
        $("body").on("click","#multiple_uploading", function () {
            var html = '';
            openAlbum(function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (imageCollection.length < IMAGE_MAX){
                        imageCollection.push(data[i].pic_path);

                        html += `<div class="multiple-item">`;
                        html += `<img src="${ns.img(data[i].pic_path)}" alt="">`;
                        html += `<span class="icon">x</span>`;
                        html += `</div>`;
                    }
                }
                if (imageCollection.length >= IMAGE_MAX)
                    $("#multiple_uploading").addClass("layui-hide");

                $(".multiple-uploading").append(html);

            }, IMAGE_MAX);
        });

        $("body").on('click', '.multiple-item .icon', function () {
            var index = $(this).parent().index() - 1;
            imageCollection.splice(index,1);
            $(this).parent().remove();

            if (imageCollection.length < IMAGE_MAX)
                $("#multiple_uploading").removeClass("layui-hide");
        });

        //商品亮点
        $("body").on("click", ".shopping-lightspot .add-lightspot", function () {
            var len = 0,
                    html = "";

            html += `<div class="lightspot-item">`;
            html += `亮点名称：<input type="text" class="layui-input" maxlength="10" placeholder="最多10个字">`;
            html += `<span>x</span>`;
            html += `</div>`;
            $(this).before(html);

            len = $(".shopping-lightspot .lightspot-item").length;
            if(len >= 3)
                $(this).addClass("layui-hide");
        });

        $("body").on("click", ".shopping-lightspot .lightspot-item span", function () {
            $(this).parents(".lightspot-item").remove();

            var len = $(".shopping-lightspot .lightspot-item").length;
            if(len < 3) $(".shopping-lightspot .add-lightspot").removeClass("layui-hide");
        });

        /**
         * 表单提交(立即发布)
         */
        form.on('submit(save)', function(data){
            field = data.field;
            field.status = 1;
            formSubmit(field)
        });

        /**
         * 表单提交(草稿箱)
         */
        form.on('submit(saveDrafts)', function(data){
            field = data.field;
            field.status = 0;

            formSubmit(field)
        });

    });

    /**
     *  提交
     */
    function formSubmit(field)
    {
        if (selectedGoodsId.length == 0) {
            layer.msg('请选择参与活动的商品！', {icon: 5, anim: 6});
            return;
        }

        if (!imageCollection.length){
            layer.msg('请选择封面图！', {icon: 5, anim: 6});
            return;
        }
        field.cover_img = imageCollection.join();

        var goodsHighlights = [];
        for (var i = 0; i < $(".lightspot-item").length; i++){
            if($(".lightspot-item").eq(i).find('input').val()){
                goodsHighlights.push($(".lightspot-item").eq(i).find('input').val());
            }else{
                layer.msg("商品亮点不能为空");
                return false;
            }
        }
        field.goods_highlights = goodsHighlights.toString();

        if (!ue.getContent()){
            layer.msg("笔记内容不能为空");
            return false;
        }
        field.note_content = ue.getContent();

        if(repeat_flag) return;
        repeat_flag = true;

        $.ajax({
            type: 'POST',
            dataType: 'JSON',
            url: ns.url("notes://shop/notes/edit"),
            data: field,
            async: false,
            success: function(res){
                repeat_flag = false;

                if (res.code == 0) {
                    layer.confirm('编辑成功', {
                        title:'操作提示',
                        btn: ['返回列表', '继续编辑'],
                        yes: function(){

                            if(field.status == 0){
                                location.href = ns.url("notes://shop/notes/drafts");
                            }else{
                                location.href = ns.url("notes://shop/notes/lists");
                            }
                        },
                        btn2: function() {
                            location.reload();
                        }
                    });
                }else{
                    layer.msg(res.message);
                }
            }
        })
    }

    /**
     * 添加商品
     */
    var selectedGoodsId = ['{$info.goods_ids}'];
    function addGoods() {
        goodsSelect(function (res) {
            if (!res.length) return false;
            selectedGoodsId = [];
            for (var i = 0; i < res.length; i++) {
                goods_ids = res[i].goods_id;
                $("input[name=goods_ids]").val(goods_ids);
                $("#goodImg").html("<img src=" + ns.img(res[i].goods_image.split(",")[0],'small') + " >");
                $(".good-img").val(res[i].goods_image);
                $(".good-name").text(res[i].goods_name);
                $(".good-price span").text(res[i].price);
            }
            selectedGoodsId.push(goods_ids);
        }, selectedGoodsId, {mode: "spu", max_num: 1, min_num: 1, disabled:0});
    }

    function back() {
        location.href = ns.url("notes://shop/notes/lists");
    }
</script>
{/block}